package cmd

import (
	"fmt"
	"github.com/spf13/viper"
	"os"
	"roll-client/util"
	"time"

	"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
	Use: "roll-client",
	Run: Run,
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
	err := rootCmd.Execute()
	if err != nil {
		os.Exit(1)
	}
}

func init() {
	rootCmd.Flags().StringP("date", "d", "", "请输入时间")
}

// Run 开始运行
func Run(cmd *cobra.Command, args []string) {
	num := viper.GetInt("num")                         // 执行总数
	delay := viper.GetInt("delay")                     // 延迟执行时间 单位:毫秒
	platform := viper.GetStringSlice("platform")       // 平台
	phoneModels := viper.GetStringSlice("phoneModels") // 手机型号
	year := viper.GetInt("year")                       // 年
	month := viper.GetInt("month")                     // 月
	day := viper.GetInt("day")                         // 日
	hour := viper.GetInt("hour")                       // 时
	minute := viper.GetInt("minute")                   // 分
	nowYmd := viper.GetBool("nowYmd")                  // 当前年月日

	for i := 1; i <= num; i++ {
		fmt.Println(fmt.Sprintf("%-25s %-20s %-20s ip:%-20s %-20s 拦截数量：%-10s %-10s",
			util.GenerateOrderNumber(24),
			util.GetRandomElement(phoneModels),
			util.GenerateRandomSN(15),
			util.GenerateRandomIP(),
			util.GetRandomElement(platform),
			util.GenerateRandomNumber(100),
			util.GenerateDateTime(year, month, day, hour, minute, nowYmd),
		))

		// 延迟执行
		time.Sleep(time.Millisecond * time.Duration(delay))
	}
}
